home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / basic / integral.zip / SIMP.BAS next >
BASIC Source File  |  1994-03-22  |  2KB  |  44 lines

  1. $If 0
  2.  
  3.      ╔═══════════════════════════════════════════════╤═══════════════╗
  4.      ║ Program : Simpsons Rule (Evaluate an integral)│ Version : 0.0 ║
  5.      ╟────────────────────────────────┬──────────────┴───────────────╢
  6.      ║ Written by Kurt J. Wolf        │ [X] Released to public       ║
  7.      ║ Copyright 1994 by Kurt J. Wolf │ [ ] Do not release to public ║
  8.      ╟────────────────────────────────┼──────────────────────────────╢
  9.      ║ Compiler : PowerBASIC          │ Compiler Version : 3.0c      ║
  10.      ╚════════════════════════════════╧══════════════════════════════╝
  11.        Home : 39 Brentwood Rd        School : PSU - MA Box 267
  12.               Camp Hill, PA 17011             Mont Alto, PA 17237
  13.               (717) 763-8913                  (717) 749-6430
  14.                                    InterNet : kjw124@psuvm.psu.edu
  15.  
  16.      ───────────────────────────────────────────────────────< Notes >─
  17.      ---This is another method for evaluating a definit integral.  This
  18.      method will not return the same values found using the trapazoid
  19.      rule, they are two different beasts.  If you would like to use this
  20.      code, feel free, I don't mind, just drop me a line and tell me that
  21.      you are using it.  (More of an ego boost than anything!)
  22.  
  23.      ---Thank you once again, Lloyd.
  24. $Endif
  25.  
  26. function f#(x#) public
  27.         f# = x# ^ 2 + 1.0
  28. end function
  29.  
  30. function integrand#(a#, b#, n#) public
  31.         h# = (b# - a#) / n#
  32.         h2# = h# / 2
  33.         for i% = 1 to n# - 1
  34.                 x# = a# + i% * h#
  35.                 sum1# = sum1# + f#(x#)
  36.                 sum2# = sum2# + f#(x# + h2#)
  37.         Next i%
  38.         integrand# = h# * (f#(a#) + 2.0 * sum1# + f#(b#) + 4.0 * sum2#) / 6.0
  39. end function
  40.  
  41. print str$(integrand#(0, 1, 10))
  42. print str$(integrand#(0, 1, 20))
  43. print str$(integrand#(0, 1, 100))
  44.